package com.zhouyang.websocket.websockettest.config;

import com.zhouyang.websocket.websockettest.interceptor.WebSocketInterceptor;
import com.zhouyang.websocket.websockettest.websocket.WebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @Author:ZhouYang
 * @Description:
 * @Date: Created in 2020/7/13 11:16
 * @Modified By： WebSocket 配置类
 */

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketInterceptor webSocketInterceptor;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(myHandler(), "/ws").setAllowedOrigins("*")
                //添加WebSocket拦截器
                .addInterceptors(webSocketInterceptor);

    }


    @Bean
    public WebSocketHandler myHandler() { return new WebSocketHandler(); }

}
